This notebook gets the unique values from a specified field in a GRASS vector map.
This notebook uses GRASS GIS (7.0.4), and must be run inside of a GRASS environment (start the jupyter notebook server from the GRASS command line). GRASS GIS
vector_map – GRASS vector map to get unique values from
value_field – field in 'vector_map' to get unique values from
output_filename – path to export a list of unique values in csv format
In [4]:
vector_map = 'landcover_corine'
In [5]:
value_field = 'value'
In [22]:
output_filename = ""
In [9]:
import pandas
import numpy as np
import grass.script as gscript
from grass.pygrass.vector import VectorTopo
from grass.pygrass.vector.table import DBlinks
connect to attribute table
In [7]:
def connectToAttributeTable(map):
vector = VectorTopo(map)
vector.open(mode='r')
dblinks = DBlinks(vector.c_mapinfo)
link = dblinks[0]
return link.table()
query to attribute table
In [16]:
table = connectToAttributeTable(map=vector_map)
table.filters.select(value_field)
cursor = table.execute()
result = np.array(cursor.fetchall())
cursor.close()
get unique values
In [17]:
data = np.unique(result)
In [18]:
data
Out[18]:
export unique values to csv
In [21]:
np.savetxt(output_filename, data)